package com.evil.application.enums.order;

import com.evil.common.activiti.enums.HandleTypeClassifyEnum;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * 订单查询类型
 *
 * @author 15587
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum OrderQueryTypeEnum {

    /**
     * 查询类型（0：我的待办，1：已办事项，2：抄送事项，3：我发起的，4：全部数据）
     */
    PENDING(0, "我的待办", HandleTypeClassifyEnum.PROCESSING),
    HANDLED(1, "已办事项", HandleTypeClassifyEnum.HANDLED),
    SEND(2, "抄送事项", HandleTypeClassifyEnum.CARBON),
    START(3, "我发起的", HandleTypeClassifyEnum.START),
    DRAFT_BOX(5, "草稿箱", null),
    ALL(4, "全部数据", null),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final HandleTypeClassifyEnum handleTypeClassifyEnum;

    public static Optional<OrderQueryTypeEnum> isExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static OrderQueryTypeEnum findById(Integer id) {
        return OrderQueryTypeEnum.isExistById(id).orElseThrow(OrderQueryTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "查询类型参数错误");
    }

    public static Stream<OrderQueryTypeEnum> stream() {
        return Arrays.stream(values());
    }

    public static <R> Stream<R> stream(Function<OrderQueryTypeEnum, ? extends R> mapper) {
        return Arrays.stream(values()).map(mapper);
    }

    public static void forEach(Consumer<OrderQueryTypeEnum> action) {
        Arrays.asList(values()).forEach(action);
    }
}
